from threading import Thread

# 节省文件与json解释的过程，我们使用一个全局变量，全局变量会被多线程共享
data = {"count": 2}


def buy_ticket(username):
    """购买车票"""
    global data
    if data["count"] > 0:
        print(f"{username}购买车票成功!")
        data["count"] -= 1
    else:
        print(f"{username}购买车票失败!")


if __name__ == '__main__':
    for i in range(10):
        p = Thread(target=buy_ticket, args=(f"user-{i}",))
        p.start()
